package com.mocchira.music.radio;

import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;

import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class SharePopupWindow extends PopupWindow {
    
    Context context = null;
    LayoutInflater inflater = null;
    LinearLayout viewRoot = null;

    static private Map<String, SharePopupWindow> instances = new WeakHashMap<String, SharePopupWindow>();
    
    private SharePopupWindow(Context ctx, String text) {
        super(ctx);
        context = ctx;
        inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        viewRoot = new LinearLayout(context);
        viewRoot.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        viewRoot.setOrientation(LinearLayout.VERTICAL);
        
        // Query the system for matching activities.
        final Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, text);
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
        int numActivities = activities.size();
        for (int i = 0; i != numActivities; ++i) {
            final ResolveInfo info = activities.get(i);
            String label = info.loadLabel(pm).toString();
            inflateSharingMenu(label, info.loadIcon(pm), info, intent);
        }
        setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        setContentView(viewRoot);
        //setBackgroundDrawable(context.getResources().getDrawable(R.drawable.gd_quick_action_bar_item_normal));
        setTouchable(true);
        setOutsideTouchable(true);
    }
    
    private void inflateSharingMenu(String label, Drawable icon, ResolveInfo info, Intent intent) {
        inflater.inflate(R.layout.pop_menu, viewRoot);
        LinearLayout child = (LinearLayout) viewRoot.getChildAt(viewRoot.getChildCount() - 1);
        ImageView image = (ImageView) child.findViewById(R.id.icon);
        image.setImageDrawable(icon);
        TextView text = (TextView) child.findViewById(R.id.name);
        text.setText(label);
        if (info != null) {
            final Intent resolvedIntent = new Intent(intent);
            ActivityInfo ai = info.activityInfo;
            resolvedIntent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));
            child.setTag(resolvedIntent);
        } else {
            child.setTag(intent);
        }
        child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                Intent intent = (Intent) v.getTag();
                context.startActivity(intent);
            }
        });
    }
    
    public static SharePopupWindow getInstance(Context ctx, String text) {
        SharePopupWindow popup = instances.get(text);
        if (popup == null) {
            popup = new SharePopupWindow(ctx, text);
            instances.put(text, popup);
        }
        return popup;
    }

    public void show(View parent) {
        int pw = parent.getWidth();
        int ph = parent.getHeight();
        // force to compute actual w/h
        View content = getContentView();
        content.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        // centerized
        int offsetx = (pw - content.getMeasuredWidth()) / 2;
        int offsety = (ph - content.getMeasuredHeight()) / 2 - ph;
        super.showAsDropDown(parent, offsetx, offsety);
    }

}
